package com.sheng.whyclass.person.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.toolbox.StringRequest;
import com.sheng.util.Constant;
import com.sheng.util.CustomToast;
import com.sheng.util.HttpProcessManager;
import com.sheng.util.VolleyManager;
import com.sheng.whyclass.R;
import com.sheng.whyclass.person.event.TeacherRegisterEvent;

import de.greenrobot.event.EventBus;

public class TeacherRegisterActivity extends Activity implements View.OnClickListener{

    private TextView title;
    private EditText teacherName;
    private EditText teacherPass;
    private EditText teacherDepartment;
    private Button teacherRegisterBtn;

    private String teacher_name;
    private String teacher_pass;
    private String teacher_department;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_teacher_register);
        EventBus.getDefault().register(this);
        initView();
    }



    private void initView() {
        title = (TextView) findViewById(R.id.title);
        teacherName = (EditText) findViewById(R.id.teacher_name);
        teacherPass = (EditText) findViewById(R.id.teacher_pass);
        teacherDepartment = (EditText) findViewById(R.id.teacher_department);
        teacherRegisterBtn = (Button) findViewById(R.id.teacher_register_btn);
        teacherRegisterBtn.setOnClickListener(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.teacher_register_btn:
                teacher_name = teacherName.getText().toString().trim();
                teacher_pass = teacherPass.getText().toString().trim();
                teacher_department = teacherDepartment.getText().toString().trim();

                if (TextUtils.isEmpty(teacher_name) || TextUtils.isEmpty(teacher_pass)||TextUtils.isEmpty(teacher_department)) {
                    CustomToast.showLongToast(getString(R.string.para_null));
                    return;
                }
                register_teacher(teacher_name, teacher_pass, teacher_department);
                break;
        }
    }

    private void register_teacher(String teacher_name, String teacher_pass, String teacher_department) {

        StringRequest request = HttpProcessManager.getInstance().registerTeacher(Constant.HOST_TEACHER_REGISTER, teacher_name, teacher_pass,teacher_department);
        VolleyManager.getInstance(getApplicationContext()).addToRequestQueue(request);
    }

    public void onEventMainThread(TeacherRegisterEvent event) {


        int code = event.code;
        if (code == Constant.CODE_FAIL) {
            CustomToast.showToast(getString(R.string.net_fail));

            return;
        } else {
            if(code==Constant.CODE_SUCCESS){
                startActivity(new Intent(TeacherRegisterActivity.this, TeacherLoginActivity.class));
                finish();
            }
            CustomToast.showToast(event.info);
        }

    }
}
